#ifndef MAINWIDGET_H
#define MAINWIDGET_H

#include <QApplication>
#include <QButtonGroup>
#include <QDebug>
#include <QGridLayout>
#include <QKeyEvent>
#include <QLabel>
#include <QMessageBox>
#include <QPushButton>
#include <QSettings>
#include <QWidget>
#include "sleeper.h"
#include "logdialog.h"
#include "serialportdialog.h"
#include "relaybutton.h"

#define SETTINGS_FILENAME   (QApplication::applicationDirPath() + "/Settings.ini")
#define LOGMSG_FILENAME     (QApplication::applicationDirPath() + "/LogMsg.log")

class MainWidget : public QWidget
{
    Q_OBJECT

public:
    MainWidget(QWidget *parent = 0);
    ~MainWidget();

Q_SIGNALS:
    void log(const QString msg);
    void save(const QString fileName);

protected:
    virtual void keyPressEvent(QKeyEvent *e);

private slots:
    void buttonClicked(int id);

private:
    enum {
        IdButtonSerialPort = 1000, IdButtonExit,
        IdButtonRelay = 2000,
    };

    LogDialog           *m_pLogDialog;
    QList<BoardData>    m_listBoardData;
    SerialPortData      m_serialPortData;
    QButtonGroup        *m_pButtonGroup;
    QSerialPort         *m_pSerialPort;

    void logDialogInit();

    void settingsLoad(const QString fileName);
    void settingsSave(const QString fileName);

    void widgetInit();

    void serialPortInit();
    void serialPortOpen();
    void serialPortClose();
    void serialPortWrite(const QByteArray &data);
private slots:
    void serialPortRead();
    void serialPortError(QSerialPort::SerialPortError error);
};

#endif // MAINWIDGET_H
